package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.UserImage;
import com.chixing.ih.module.programmer.service.ImageService;

import com.chixing.ih.util.FileServerUtil;
import com.chixing.ih.util.FileUtil;
import com.chixing.ih.util.RequestIdUtil;
import com.chixing.ih.util.UUIDUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘岩
 * Date: 2020-10-02
 * Time: 21:04
 *
 * @version 1.0
 */
@RestController
@RequestMapping("/image")
@Api(tags = "程序员模块上传图片相关Api")
public class ImageController {

    @Autowired
    ImageService imageService;

    /**
     * desc:上传用户图片接口
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "上传用户图片接口", notes = "程序员上传自己头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传的图片资源", required = true, dataType = "multipartFile"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "上传成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })

    @PostMapping("/uploadUser")
    public Map<String, Object> upLoadUserImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        String type = FileUtil.getType(file);
        String uuId = UUIDUtil.get16UUId();
        String url = FileServerUtil.uploadFile(file, uuId + type);
        UserImage userImage = new UserImage();
        userImage.setImageUrl(url);
        userImage.setUserId(myId);
        boolean flag = imageService.updateUserImage(userImage);
        if (flag)
            return ResponseData.REP_GET_SUCCESS(userImage);
        else
            return ResponseData.REP_INVALID_ERROR();

    }

//    /**
//     * desc:上传项目图片接口
//     *
//     * @param file
//     * @param projectId
//     * @return
//     */
//    @ApiOperation(value = "上传项目图片接口", notes = "程序员上传项目图片")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "file", value = "上传的图片资源", required = true, dataType = "multipartFile"),
//            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "int"),
//
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "上传成功"),
//            @ApiResponse(code = 400, message = "查询失败"),
//    })
//
//    @RequestMapping("uploadProject")
//    public Map<String, Object> upLoadProject(@RequestParam("file") MultipartFile file, @RequestParam("projectId") int projectId) {
//        String type = FileUtil.getType(file);
//        String uuId = UUIDUtil.get16UUId();
//        String url = FileServerUtil.uploadFile(file, uuId + type);
//        ProjectImage projectImage = new ProjectImage();
//        projectImage.setImageUrl(url);
//        projectImage.setProjectId(projectId);
//        boolean flag = imageService.updateProjectImage(projectImage);
//        if (flag)
//            return ResponseData.REP_GET_SUCCESS(projectImage);
//        else
//            return ResponseData.REP_INVALID_ERROR();
//
//    }


}
